package com.tools.developer.app;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Debug.MemoryInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.tools.developer.DataBaseActivity;
import com.tools.developer.R;
import com.tools.developer.SignatureActivity;
import com.tools.developer.util.Env;

public class AppDetailActivity4Running extends ListActivity implements
		OnItemClickListener, OnClickListener {

	public static final String PKG_NAME = "pkg_name";
	private String mPkgName;
	private String[] mItems = { MEM, SINATURE, OPEN_DATABASE };
	private static final String SINATURE = "show sinature";
	private static final String MEM = "内存信息";
	private static final String OPEN_DATABASE = "open database";
	public static final String APP_MODEL = "app_model";
	private AppModel mApp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_detail_running);

		if (getIntent() != null) {
			AppModel app = getIntent().getParcelableExtra(APP_MODEL);
			if (app != null) {
				mApp = app;
				mPkgName = mApp.packageName;

				MemoryInfo[] memInfo = AppUtil.getProcessMemoryInfo(mPkgName,
						this.getApplicationContext());
				String memStr = null;
				if (memInfo != null) {
					for (MemoryInfo mem : memInfo) {
						memStr = "dalvikPrivateDirty:" + mem.dalvikPrivateDirty
								+ "\r\n";
						memStr = memStr + "dalvikPss:" + mem.dalvikPss + "\r\n";
						memStr = memStr + "dalvikSharedDirty:"
								+ mem.dalvikSharedDirty + "\r\n";
						memStr = memStr + "nativePrivateDirty:"
								+ mem.nativePrivateDirty + "\r\n";
						memStr = memStr + "nativePss:" + mem.nativePss + "\r\n";
						memStr = memStr + "nativeSharedDirty:"
								+ mem.nativeSharedDirty + "\r\n";
						memStr = memStr + "otherPrivateDirty:"
								+ mem.otherPrivateDirty + "\r\n";
						memStr = memStr + "otherPss:" + mem.otherPss + "\r\n";
						memStr = memStr + "otherSharedDirty:"
								+ mem.otherSharedDirty;
						memStr = memStr + "\r\n";
					}
				}
				((TextView) findViewById(R.id.txt)).setText(getString(
						R.string.app_memusage, String.valueOf(mApp.memUsage)
								+ "\r\n" + memStr));
			}

		}
		setListAdapter(new ArrayAdapter<String>(AppDetailActivity4Running.this,
				android.R.layout.simple_list_item_1, mItems));
		getListView().setOnItemClickListener(this);
		findViewById(R.id.optimize_app_detail_set).setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (position < mItems.length) {
			Intent intent = null;
			if (SINATURE.equalsIgnoreCase(mItems[position])) {
				intent = new Intent(AppDetailActivity4Running.this,
						SignatureActivity.class);
			} else if (OPEN_DATABASE.equalsIgnoreCase(mItems[position])) {
				intent = new Intent(AppDetailActivity4Running.this,
						DataBaseActivity.class);
			} else if (MEM.equalsIgnoreCase(mItems[position])) {

			}
			if (intent != null) {
				intent.putExtra(PKG_NAME, mPkgName);
				this.startActivity(intent);
			}
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.optimize_app_detail_set:
			startActivity(Env.getPackageDetailsIntent(mPkgName));
			break;
		}

	}
}
